package com.dminvoicemanager.itemmaster;

import com.dminvoicemanager.R;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class DM_Items_Information extends Activity {

	private static final String TAG = null;
	private SQLiteDatabase mydb;
	private String DBNAME = "dminvoicemanager.db";
	String CurrentRefItem;
	EditText dmitmcode, dmitemname, dmitmtype, dmitmbrand, dmitmsubbrand,
			dmitmcategory, dmitmstatus, dmitmstateofmeasure, dmitmtonnage,
			dmitmnoucode;

	public void btnBack(View view) {
		Intent myIntent = new Intent(view.getContext(), DM_Item_Master.class);
		myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
				| Intent.FLAG_ACTIVITY_NEW_TASK);
		startActivityForResult(myIntent, 0);
		finish();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dm_items_information);
		TextView txtHeading = (TextView) findViewById(R.id.txtHeading);
		txtHeading.setText("Items Information");
		dmitmcode = (EditText) findViewById(R.id.dmitmcode);
		dmitemname = (EditText) findViewById(R.id.dmitemname);
		dmitmtype = (EditText) findViewById(R.id.dmitmtype);
		dmitmbrand = (EditText) findViewById(R.id.dmitmbrand);
		dmitmsubbrand = (EditText) findViewById(R.id.dmitmsubbrand);
		dmitmcategory = (EditText) findViewById(R.id.dmitmcategory);
		dmitmstatus = (EditText) findViewById(R.id.dmitmstatus);
		dmitmstateofmeasure = (EditText) findViewById(R.id.dmitmstateofmeasure);
		dmitmtonnage = (EditText) findViewById(R.id.dmitmtonnage);
		dmitmnoucode = (EditText) findViewById(R.id.dmitmnoucode);
		selectCurrentItemCode();

	}

	private void selectCurrentItemCode() {
		// TODO Auto-generated method stub
		SQLiteDatabase mydb;
		mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE, null);
		Cursor curCurrentItems = null;
		try {
			String query = "SELECT * from FItemsMasterSum";
			curCurrentItems = mydb.rawQuery(query, null);
		} catch (Exception e) {
		}
		if (curCurrentItems != null) {
			if (curCurrentItems.getCount() > 0) {
				while (curCurrentItems.moveToNext()) {
					CurrentRefItem = curCurrentItems.getString(1);
					Log.v(TAG, "CurrentRefItem XXX :" + CurrentRefItem);
					filterItemsInfomation();
				}
			}
		}
		curCurrentItems.close();
		mydb.close();
	}

	private void filterItemsInfomation() {
		// TODO Auto-generated method stub
		mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE, null);
		Cursor chkItemdDet = null;
		try {
			String queryChkItemInfo = "SELECT FI.ItemCode,FI.ItemName,FI.TypeCode,FI.BrandCode,FI.SBrandCode,FI.GroupCode,FI.UnitCode,FI.SKUCode,FI.NOUCase,FB.BrandName,FSB.SBrandName,FG.GroupName,FT.TypeName,FS.Tonnage FROM fItems FI INNER JOIN FType FT ON FT.TypeCode = FI.TypeCode INNER JOIN FBrand FB ON FB.BrandCode = FI.BrandCode INNER JOIN FSubBrand FSB ON FSB.SBrandCode = FI.SBrandCode INNER JOIN FGroup FG ON FG.GroupCode = FI.GroupCode INNER JOIN FSKU FS ON FS.SKUCode = FI.SKUCode Where FI.ItemCode = '"
					+ CurrentRefItem + "'";

			chkItemdDet = mydb.rawQuery(queryChkItemInfo, null);
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		if (chkItemdDet != null) {
			if (chkItemdDet.getCount() > 0) {
				while (chkItemdDet.moveToNext()) {
					//String strItemCode = chkItemdDet.getString(0);
					String strItemCode = chkItemdDet.getString(0);
					String strItemName = chkItemdDet.getString(1);
					String strTypeCode = chkItemdDet.getString(2);
					String strBrandCode = chkItemdDet.getString(3);
					String strSBrandCode = chkItemdDet.getString(4);
					String strGroupCode = chkItemdDet.getString(5);
					String strUnitCode = chkItemdDet.getString(6);
					String strSKUCode = chkItemdDet.getString(7);
					String strNOUCase = chkItemdDet.getString(8);
					String strBrandName = chkItemdDet.getString(9);
					String strSBrandName = chkItemdDet.getString(10);
					String strGroupName = chkItemdDet.getString(11);
					String strTypeName = chkItemdDet.getString(12);
					String strTonnage = chkItemdDet.getString(13);

					dmitmcode.setText(strItemCode);
					dmitemname.setText(strItemName);
					dmitmtype.setText(strTypeName);
					dmitmbrand.setText(strBrandName);
					dmitmsubbrand.setText(strSBrandName);
					dmitmcategory.setText(strGroupName);
					dmitmstatus.setText("Active");
					dmitmstateofmeasure.setText(strUnitCode);
					dmitmtonnage.setText(strTonnage);
					dmitmnoucode.setText(strNOUCase);
					Log.v(TAG, "chkRefNo XXX :" + strItemCode);
				}
			}
		}
		chkItemdDet.close();
		mydb.close();
	}

	@Override
	public void onBackPressed() {
		// Do Here what ever you want do on back press;
	}
}
